<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-01-04 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'include/lib/cache.func.php';
require ROOT_PATH.'include/lib/cookie.func.php';
require ROOT_PATH.'include/lib/session.func.php';
require ROOT_PATH.'include/sql/sql_functions.php';

class masterObj
{
	var $request = array();

	var $act = '';
	var $user = array();
	var $session = array();

	var $_is_messager = false;

	var $ajhAuthKey = '';

	function __construct($request)
	{
		load_setting('config_global');

		$this->request = $request;
		$this->mod = isset($request['m']) ? $request['m'] : '';
		$this->act = isset($request['a']) ? $request['a'] : '';
		
		$this->request['limit'] = isset($request['count'])? min(500, max((int)$request['count'], 10)) : 10;
		$this->request['offset'] = isset($request['page']) ? max(0, (int)$request['page'] - 1) : 0;
		
		if($this->mod != 'login')
		{
			$this->_checkUser();
		}
	}
	
	function message($msg)
	{
		echo $msg;
		exit();
	}
	
	function jsonMessage($msg, $status)
	{
		$result['status'] = $status;
		$result['message'] = $msg;
	
		$json = json_encode($result);
				
		echo $json;
		exit();
	}
	
	function jsonResponse($response, $status='OK')
	{
		$json = '';
		if(is_array($response) && count($response) > 0)
		{
			$results = array('status' => $status, 'result'=>$response);
			$json = @json_encode($results);
		}
		
		echo $json;
		exit();
	}

	/**
	 * 
	 * Check if user login & has permission
	 */
	private function _checkUser()
	{
		// no login
		if(!isset($_SESSION['user']) || $_SESSION['user']['id'] < 1)
		{
			$this->message('Please login first.');
		}
		
		$this->user = $_SESSION['user'];
		
		return true;
	}
}